home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / AmigaSystem / Scalos / GuiGFXLib / src / guigfx_convolve.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  3.9 KB  |  173 lines

  1. /*********************************************************************
  2. ----------------------------------------------------------------------
  3.  
  4.     guigfx_convolve
  5.  
  6. ----------------------------------------------------------------------
  7. *********************************************************************/
  8.  
  9. #include <render/render.h>
  10. #include <render/renderhooks.h>
  11. #include <utility/tagitem.h>
  12. #include <graphics/gfx.h>
  13. #include <graphics/view.h>
  14. #include <exec/memory.h>
  15.  
  16. #include <proto/render.h>
  17. #include <proto/graphics.h>
  18. #include <proto/exec.h>
  19. #include <proto/utility.h>
  20. #include <proto/dos.h>
  21. #include <proto/cybergraphics.h>
  22. #include <libraries/cybergraphics.h>
  23.  
  24. #include <guigfx/guigfx.h>
  25.  
  26. #include "guigfx_global.h"
  27. #include "guigfx_convolve.h"
  28. #include "guigfx_picturemethod.h"
  29.  
  30. #define DEFAULT_SPATSIZEX    1
  31. #define DEFAULT_SPATSIZEY    1
  32.  
  33.  
  34. int defaultspatkernel_blur[(DEFAULT_SPATSIZEX * 2 + 1) * (DEFAULT_SPATSIZEY * 2 + 1)] =
  35. {
  36.     0.0*255,    1.0*255,    0.0*255,
  37.     1.0*255,    2.0*255,    1.0*255,
  38.     0.0*255,    1.0*255,    0.0*255
  39.     // div: 6
  40.     // inc: 0
  41. };
  42.  
  43. int defaultspatkernel_emboss[(DEFAULT_SPATSIZEX * 2 + 1) * (DEFAULT_SPATSIZEY * 2 + 1)] =
  44. {
  45.     0,0,0,
  46.     0,99,0,
  47.     0,0,-99
  48.     // div: 1
  49.     // inc: 204
  50. };
  51.  
  52. int defaultspatkernel_rise[(DEFAULT_SPATSIZEX * 2 + 1) * (DEFAULT_SPATSIZEY * 2 + 1)] =
  53. {
  54.     0.0*255,    0.0*255,    0.0*255,
  55.     0.0*255,    2.0*255,    0.0*255,
  56.     0.0*255,    0.0*255,    -1.0*255
  57.     // div: 1
  58.     // inc: 0
  59. };
  60.  
  61. int defaultspatkernel_sharpen[(DEFAULT_SPATSIZEX * 2 + 1) * (DEFAULT_SPATSIZEY * 2 + 1)] =
  62. {
  63.     00,    -1,    00,
  64.     -1,    10,    -1,
  65.     00,    -1,    00
  66.     // div: 6
  67.     // inc: 0
  68. };
  69.  
  70. KERNEL defaultkernel =
  71. {
  72.     {1.0,0.0,0.0,
  73.      0.0,1.0,0.0,
  74.      0.0,0.0,1.0,
  75.      0.0,0.0,0.0},
  76. //    defaultspatkernel_sharpen,
  77.     defaultspatkernel_emboss,
  78.     DEFAULT_SPATSIZEX, DEFAULT_SPATSIZEY,
  79.     BORDERMODE_CONTINOUS,
  80.     0x0,
  81.     1, 0
  82. };
  83.  
  84. #define    GETRGB(b,x,y)    ((b) + ((((x) + destwidth) % destwidth) + (((y) + destheight) % destheight) * destwidth))
  85. #define    GETSPAT(b,x,y) ((b)->spatkernel + ((x) + (b)->spatwidth) + ((y) + (b)->spatheight) * ((b)->spatwidth * 2 + 1))
  86.  
  87.  
  88. ULONG PIC_Convolve(PIC *pic, KERNEL *kernel, TAGLIST tags)
  89. {
  90.     BOOL success = FALSE;
  91.     UWORD destwidth, destheight, destx, desty;
  92.  
  93.     if (pic)
  94.     {
  95.         if (!kernel) kernel = &defaultkernel;
  96.  
  97.         ObtainSemaphore(&pic->semaphore);
  98.  
  99.         destwidth = GetTagData(GGFX_DestWidth, pic->width, tags);
  100.         destheight = GetTagData(GGFX_DestHeight, pic->height, tags);
  101.         destx = GetTagData(GGFX_DestX, 0, tags);
  102.         desty = GetTagData(GGFX_DestY, 0, tags);
  103.  
  104.  
  105.         if (ExtractAlphaArray(pic))
  106.         {
  107.             if (PIC_Render(pic, PIXFMT_0RGB_32, NULL))
  108.             {
  109.                 ULONG *dest;
  110.                 ULONG *source = ((ULONG *) pic->array) + destx + desty * pic->width;
  111.                 
  112.                 if (dest = AllocRenderVec(MemHandler, destwidth * destheight * 4))
  113.                 {
  114.                     int x,y,c,d;
  115.                     ULONG *sbufptr = source;
  116.                     ULONG *dbufptr = dest;
  117.                     int SUMR, SUMG, SUMB;
  118.                     int k, div, inc;
  119.                     ULONG rgb;
  120.                     
  121.                     div = kernel->divisor;
  122.                     inc = kernel->increment;
  123.                     
  124.                     for (y = 0; y < destheight; y++) 
  125.                     {
  126.                         for (x = 0; x < destwidth; x++) 
  127.                         {
  128.                             SUMR = 0;
  129.                             SUMG = 0;
  130.                             SUMB = 0;
  131.                             
  132.                             for (d = -kernel->spatheight; d < kernel->spatheight + 1; d++)
  133.                             {
  134.                                 for (c = -kernel->spatwidth; c < kernel->spatwidth + 1; c++)
  135.                                 {
  136.                                     k = *GETSPAT(kernel, c, d);
  137.                                     rgb = *GETRGB(sbufptr, x + c, y + d);
  138.                                     
  139.                                     SUMR += ((rgb & 0xff0000) >> 16) * k;
  140.                                     SUMG += ((rgb & 0x00ff00) >> 8) * k;
  141.                                     SUMB += (rgb & 0x0000ff) * k;
  142.                                 }
  143.                             }
  144.                             
  145.                             *dbufptr++ =    ((RNG(0, (SUMR / div), 255)) << 16) +
  146.                                                         ((RNG(0, (SUMG / div), 255)) << 8) +
  147.                                                          RNG(0, (SUMB / div), 255);
  148.                         }
  149.                     }
  150.                 
  151.                     dbufptr = dest;    
  152.                     for (y = 0; y < destheight; ++y)
  153.                     {
  154.                         for (x = 0; x < destwidth; ++x)
  155.                         {
  156.                             *source++ = *dbufptr++;
  157.                         }
  158.                         dbufptr += destwidth - pic->width;
  159.                     }
  160.     
  161.                     FreeRenderVec(dest);
  162.                 }
  163.     
  164.                 success = TRUE;
  165.             }
  166.         }
  167.     }
  168.  
  169.     ReleaseSemaphore(&pic->semaphore);
  170.  
  171.     return (ULONG) success;
  172. }
  173.